package com.orchard.xpz.service.gardenMedication.Impl;

import com.github.pagehelper.PageHelper;
import com.orchard.xpz.dao.GardenMedicationDao;
import com.orchard.xpz.entity.GardenFer.GardenFer;
import com.orchard.xpz.entity.GardenMedication.GardenMedication;
import com.orchard.xpz.service.gardenMedication.GardenMedicationService;
import com.orchard.xpz.utils.PageResultData;
import com.orchard.xpz.utils.UserRuntime;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/21 15:34
 **/
@Service
public class GardenMedicationServiceImpl implements GardenMedicationService {

    @Autowired
    private GardenMedicationDao gardenMedicationDao;

    @Override
    public PageResultData<GardenMedication> getPageList(GardenMedication gardenMedication) {
        PageHelper.startPage(gardenMedication.getPageNum(),gardenMedication.getPageSize());
        List<GardenMedication> list = gardenMedicationDao.getList(gardenMedication);
        PageResultData<GardenMedication> pageResultData = new PageResultData<>(list, gardenMedication.getPageNum(), gardenMedication.getPageSize());
        return pageResultData;
    }

    @Override
    public boolean saveOrUpdate(GardenMedication gardenMedication) {
        gardenMedication.setGardenId(gardenMedication.getGardenPlot().getGardenId());
        gardenMedication.setPesticidesId(gardenMedication.getPesticidesBuy().getPesticidesId());
        if (ObjectUtils.isEmpty(gardenMedication.getMedId())){
            gardenMedication.setCreateName(UserRuntime.getUser().getUserName());
            gardenMedication.setCreateTime(LocalDate.now());
            int i = gardenMedicationDao.insertSelective(gardenMedication);
            return i > 0? true : false;
        }else {
            gardenMedication.setUpdateTime(LocalDate.now());
            gardenMedication.setUpdateName(UserRuntime.getUser().getUserName());
            int i = gardenMedicationDao.updateByPrimaryKeySelective(gardenMedication);
            return i > 0? true : false;
        }
    }

    @Override
    public boolean deltePlot(String id) {
        return gardenMedicationDao.deleteByPrimaryKey(id) > 0? true : false;
    }
}
